package utils.command;

import lombok.SneakyThrows;

import java.io.*;

import static utils.screenCtrl.ScreenCtrlLinux.getRegEx;

public class testC {
    public static void main(String[] args) throws IOException {
        /*String[] argss = new String[]{"/bin/bash", "-c", "xev -root"};
        Process process = Runtime.getRuntime().exec(argss);*/
        tt tt = new tt();
        tt.start();
        File file = new File("/bin");
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("/bin/bash", null, file);
        OutputStream outputStream = process.getOutputStream();
        PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream));
        printWriter.println("xev -root");
        printWriter.println("exit");
        printWriter.flush();
        BufferedReader bufferedReaderInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
        BufferedReader bufferedReaderError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        while (true) {
            String line = bufferedReaderInput.readLine();
            if (line != null && getRegEx("PropertyNotify event", line) != null) {
                line = bufferedReaderInput.readLine();
                if (getRegEx("(XKLAVIER_ALLOW_SECONDARY)", line) != null) {
                    System.out.println(tt.IsDesk());
                }
            }
            /*System.out.println(line);
            System.out.println(tt.IsDesk());*/
        }
    }
}

class tt extends Thread {
    Runtime runtime;
    BufferedReader bufferedReaderInput;

    @SneakyThrows
    @Override
    public void run() {
        runtime = Runtime.getRuntime();
    }

    public boolean IsDesk() throws IOException {
        Process process = runtime.exec("xdotool getmouselocation");
        bufferedReaderInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line, s = "";
        while ((line = bufferedReaderInput.readLine()) != null) {
            s += line;
        }
        bufferedReaderInput.close();
        String id = getRegEx("window:[0-9]+", s).replace("window:", "");
        id = "0x" + Integer.toHexString(Integer.parseInt(id));
        System.out.println(id);
        /*String[] strings = {"/bin/bash", "-c", "xwininfo -id " + id+" -tree"};
        System.out.println(CommandLinux.oSCommandStatic(strings));*/
        return id.equals("0x2000007");
    }
}



